const mongoose = require('mongoose');
var express = require('express')
var adressShi = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {addressShijiService } = require('../../service/impl/addressShijiService/index');


adressShi.post('/post/addressShi/add', async (req,res) => {
  let result
  try {
    let news  = req.body.news
    result =  await addressShijiService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  adressShi.post('/post/addressShi/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await addressShijiService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  adressShi.post('/post/addressShi/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await addressShijiService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  adressShi.post('/post/addressShi/query', async (req,res) => {
    let result
    try {
      let fills = {
        "key1":{
          "path":"shengjiID",
        }
      }
      result =  await addressShijiService.getIns().find(null,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  // // PJ02-根据省ID,查询包含的市
  adressShi.post('/post/shiji/queryShiByShengID', async (req,res) => {
    let result
    let fills = {
      "key1":{
        "path":"shengjiID",
      }
    }
    try {
      let conditions = req.body.conditions
      result =  await addressShijiService.getIns().find(conditions,null,fills)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })



  
  module.exports= adressShi